/**
 * EncodingFilter.java
 * 
 * Copyright(C)2010 FOUNDER CORPORATION
 * 
 */

package org.iori.luodan.servlet;

import java.io.IOException;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * [クラス名]<br>
 * EncodingFilter.java<br>
 * [機能概要]<br>
 * <br>
 * <br>
 * [変更履歴]<br>
 * 2011/09/21 ver1.00 新規作成<br>
 * 
 * @author FOUNDER CORPORATION zhang_jiajun
 * @version 1.00
 */
public class EncodingFilter implements Filter{
	protected String encoding = null;
	protected FilterConfig filterConfig = null;
	protected boolean ignore = true;

	/**
	 * @see javax.servlet.Filter#void ()
	 */
	public void destroy()
	{
		this.encoding = null;
		this.filterConfig = null;
	}

	/**
	 * @see javax.servlet.Filter#void (javax.servlet.ServletRequest,
	 *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws ServletException, IOException
	{
		if ((req.getCharacterEncoding() == null))
		{
			String encoding = selectEncoding(req);

			if (encoding != null)
				req.setCharacterEncoding(encoding);
		}

		//Pass control on to the next filter
		chain.doFilter(req, resp);
	}

	/**
	 * Method init.
	 *
	 * @param config
	 * @throws javax.servlet.ServletException
	 */
	public void init(FilterConfig config) throws ServletException
	{
		this.filterConfig = config;
		this.encoding = filterConfig.getInitParameter("encoding");
		String value = filterConfig.getInitParameter("ignore");

		if (value == null)
			this.ignore = true;
		else if (value.equalsIgnoreCase("true")
				|| value.equalsIgnoreCase("yes"))
			this.ignore = true;
		else
			this.ignore = false;
	}

	protected String selectEncoding(ServletRequest request)
	{
		return (this.encoding);
	}
}
